import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();

Page({
  data: {
    delBtnShow: false,
    type: 'add',
    isdaterange: false,
    isTimePas: false,
    startDate: "",
    endDate: "",
    startTime: dayjs().format('HH:mm:ss'),
    endTime: dayjs().format('HH:mm:ss'),
    form: {
      name: '',
      ftyModelName: '',
      ftyModelId: '',
      ftyModelValue: '',
      beginEffectiveDate: '',
      endEffectiveDate: '',
      beginEndEffectiveDate: '',
      beginTime: '',
      endTime: '',
      beginEndTime: '',
      description: '',
      nonProdTimeMgtDTOList: []
    }
  },

  onLoad(query) {
    console.log(query.type)
    this.data.query = query
    if (query.type == 'add') {
      dd.setNavigationBar({
        title: '新增班次'
      })
      this.setData({
        delBtnShow: false
      })
      this.getStorage()
    }
    if (query.type == 'edit') {
      this.getFactoryCalendarShiftGetById(query.id)
      dd.setNavigationBar({
        title: '编辑班次'
      })
      this.setData({
        delBtnShow: true
      })
    }
    this.setData({
      type: query.type
    })
  },

  onShow() {
    if (this.data.query.type == 'edit') {
      this.getStorage()
    }
  },

  selectRange() {
    dd.navigateTo({
      url: '/pages/selectRange/selectRange?type=back'
    })
  },

  selectDate() {
    this.setData({
      isdaterange: true
    })
  },

  selectTime() {
    this.setData({
      isTimePas: true
    })
  },

  // 日期选择返回
  onAffirm(data) {
    if (data.result) {
      this.data.form.beginEffectiveDate = data.startDate
      this.data.form.endEffectiveDate = data.endDate
      this.setData({
        "startDate": data.startDate,
        "endDate": data.endDate,
        "form.beginEndEffectiveDate": data.startDate + "至" + data.endDate
      })
    }
    this.setData({
      isdaterange: false
    })
  },

  onSelectTime(data) {
    console.log(data)
    if (data.result) {
      this.data.form.beginTime = data.startTime
      this.data.form.endTime = data.endTime
      this.setData({
        "startTime": data.startTime,
        "endTime": data.endTime,
        "form.beginEndTime": data.startTime + "至" + data.endTime
      })
    }
    this.setData({
      isTimePas: false
    })
  },

  toNonProdTime() {
    dd.navigateTo({
      url: '/pages/nonProdTime/nonProdTime'
    })
  },

  // 查询班次详细
  getFactoryCalendarShiftGetById(id) {
    let that = this
    $http(apiSetting.factoryCalendarShiftGetById, {
      id: id
    }).then((data) => {
      console.log(data.result)
      data.result.beginEndTime = data.result.beginTime + "至" + data.result.endTime
      if (data.result.beginEndEffectiveDate) {
        data.result.beginEndEffectiveDate = data.result.beginEffectiveDate + "至" + data.result.endEffectiveDate
      }
      that.setData({
        form: data.result,
        startTime: data.result.beginTime,
        endTime: data.result.endTime,
        startDate: data.result.beginEffectiveDate,
        endDate: data.result.endEffectiveDate,
      })
      dd.getStorage({
        key: 'selectFacScope',
        success: function (res) {
          that.data.selectFacScope = res.data[0]
          that.setData({
            'form.ftyModelName': res.data[0].title
          })
        },
      });
    })
  },

  save() {
    if (this.data.delBtnShow) {
      this.update()
    } else {
      this.add()
    }
  },

  add() {
    let that = this
    $http(apiSetting.factoryCalendarShiftAdd, {
      ftyModelId: that.data.form.ftyModelId, //⼯⼚模型ID
      beginEffectiveDate: that.data.form.beginEffectiveDate, //开始生效时间
      endEffectiveDate: that.data.form.endEffectiveDate, //结束生效时间
      beginTime: that.data.form.beginTime, //开始时间
      endTime: that.data.form.endTime, //结束时间
      ftyModelValue: that.data.form.ftyModelValue, //⼯⼚模型层级值，⼯⼚1，⻋间2，产线3，加⼯中⼼4，设备5
      name: that.data.form.name, //名称
      description: that.data.form.description, //描述
      nonProdTimeMgtDTOList: that.data.form.nonProdTimeMgtDTOList
    }).then((data) => {
      if (data.code == 0) {
        const pages = getCurrentPages();
        let currPage = pages[pages.length - 1]; //当前页面
        let prevPage = pages[pages.length - 2]; //上一个页面
        prevPage.data.isGetAfreshData = true;
        dd.navigateBack({
          delta: 1
        })
      }
    })
  },

  update() {
    let that = this
    $http(apiSetting.factoryCalendarShiftUpdate, {
      id: that.data.form.id,
      ftyModelId: that.data.form.ftyModelId, //⼯⼚模型ID
      beginEffectiveDate: that.data.form.beginEffectiveDate, //开始生效时间
      endEffectiveDate: that.data.form.endEffectiveDate, //结束生效时间
      beginTime: that.data.form.beginTime, //开始时间
      endTime: that.data.form.endTime, //结束时间
      ftyModelValue: that.data.form.ftyModelValue, //⼯⼚模型层级值，⼯⼚1，⻋间2，产线3，加⼯中⼼4，设备5
      name: that.data.form.name, //名称
      description: that.data.form.description, //描述
      nonProdTimeMgtDTOList: that.data.form.nonProdTimeMgtDTOList
    }).then((data) => {
      if (data.code == 0) {
        const pages = getCurrentPages();
        let currPage = pages[pages.length - 1]; //当前页面
        let prevPage = pages[pages.length - 2]; //上一个页面
        prevPage.data.isGetAfreshData = true;
        dd.navigateBack({
          delta: 1
        })
      }
    })
  },

  // 删除
  del() {
    let that = this
    dd.confirm({
      title: '温馨提示',
      content: '删除不可恢复，你确定要删除？',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (result) => {
        $http(apiSetting.factoryCalendarShiftDelete, {
          id: that.data.form.id
        }).then((data) => {
          console.log(data)
        })
        const pages = getCurrentPages();
        let currPage = pages[pages.length - 1]; //当前页面
        let prevPage = pages[pages.length - 2]; //上一个页面
        prevPage.data.isGetAfreshData = true;
        dd.navigateBack({
          delta: 1
        })
      },
    });
  },

  onInputName(e) {
    this.data.form.name = e.detail.value
  },
  descriptionInp(e) {
    this.data.form.description = e.detail.value
  },

  getStorage() {
    let that = this
    dd.getStorage({
      key: 'selectFacScope',
      success: function (res) {
        that.data.selectFacScope = res.data[0]
        that.setData({
          'form.ftyModelId': res.data[0].key,
          'form.ftyModelName': res.data[0].title,
          'form.ftyModelValue': res.data[0].level + 1,
        })
      },
    });
  }
});
